package com.game;

public class Pokdeng {
	private Deck deck = new Deck();
	private String name = "";
	private Player banker;
	private Player player;
	private int turn = 0;
	private int rule = 0;
	private int profits = 0;
	private boolean ender = false;

	public Pokdeng(String player) {
		this.name = player;
		this.profits = 0;
	}

	public void start() {
		deck.Initialize();
		turn = 0;
		ender = false;
		banker = new Player("Banker");
		player = new Player(name);
		for (int i = 0; i < 2; i++) {
			player.draw(deck);
			banker.draw(deck);
		}
	}

	public boolean game() {
		rule = checkRule();
		boolean status = checkStatus(rule);

		return status;
	}

	/** player draw extra card. */
	public String playerDraw() {
		player.draw(deck);
		nextTurn();

		String result = "";
		result = "X/" + player.getName() + "/" + showHand(player) + ",";
		result += "Y/" + banker.getName() + "/" + showHand(banker);
		return result;
	}

	public void nextTurn() {
		this.turn++;
	}

	public String getResult() {
		int rule = checkRule();
		changeProfits(rule);
		
		String result = getWinState(rule) + ",";
		result += "X/" + player.getName() + "/" + showHand(player) + ",";
		result += "Y/" + banker.getName() + "/" + showHand(banker);
		return result;
	}

	private String showHand(Player player) {
		String result = "";
		for (int i = 0; i < player.hand.size(); i++) {
			result += player.hand.get(i) + " (" + player.hand.get(i).value + ")/";
		}
		if (player.hand.size() < 3)
			result += "/";
		return result;
	}

	private int checkRule() {

		// first turn if anyone getting POK
		if (turn == 0) {
			if ((banker.getScore() == 8 && player.getScore() < 8) || (banker.getScore() == 9 && player.getScore() < 9)) {
				return 1; // 1 : banker POK !
			} else if ((player.getScore() == 8 && banker.getScore() < 8)
					|| (player.getScore() == 9 && banker.getScore() < 9)) {
				return 2; // 2 : player POK !
			} else if ((banker.getScore() == 8 && player.getScore() == 8)
					|| (banker.getScore() == 9 && player.getScore() == 9)) {
				return 99;
			}
		} else {
			/* if player get special win, then banker draw a card. */
			if (player.specialWin() > 0) {
				banker.draw(deck);
				if (banker.specialWin() > player.specialWin()) {
					return banker.specialWin() + 1; // banker special win
				} else if (player.specialWin() > banker.specialWin()) {
					return player.specialWin() + 2; // player special win
				} else {
					/* if player and banker are same the special win, then get hand value */
					if (banker.getHandValue() > player.getHandValue()) {
						return banker.specialWin() + 1;
					} else if (player.getHandValue() > banker.getHandValue()) {
						return player.specialWin() + 2;
					} else {
						System.out.println(player.getHandValue());
						System.out.println(banker.getHandValue());
						return 99;
					}
				}
			} else {
				if (player.getScore() >= banker.getScore()) {
					if (player.getScore() == banker.getScore() && player.getScore() > 4) {
						return 99;	// return DRAW if player score more than 4 (banker don't draw a card) 
					} else {
						banker.draw(deck);
					}
					if (banker.specialWin() > 0) {
						return banker.specialWin() + 1; // banker special win
					} else if (banker.getScore() > player.getScore()) {
						return 81; // Banker win
					} else if (player.getScore() > banker.getScore()) {
						return 82; // Player win
					} else {
						return 99; // Draw
					}
				} else if (banker.getScore() > player.getScore()) {
					return 81; // Banker win
				} else {
					return 99; // DRAW
				}
			}
		}
		return 0;
	}

	private void changeProfits(int rule) {
		if (!ender && rule != 0) {
			if (turn == 0 && (rule == 1 || rule == 2 || rule == 99)) {
				if (rule == 1)
					profits -= banker.getBounce();
				else if (rule == 2)
					profits += player.getBounce();
				ender = true;
			} else if (rule % 2 == 1) { // Banker win
				if (rule == 81) {
					profits -= banker.getBounce();
				} else if (rule == 11 || rule == 21) {
					profits -= 3;
				} else if (rule == 31 || rule == 41) {
					profits -= 5;
				}
				ender = true;
			} else if (rule % 2 == 0) {
				if (rule == 82) {
					profits += player.getBounce();
				} else if (rule == 12 || rule == 22) {
					profits += 3;
				} else if (rule == 32 || rule == 42) {
					profits += 5;
				}
				ender = true;
			}
		}
	}

	public int getProfit() {
		return profits;
	}

	public String getWinState(int rule) {
		String temp = "";
		switch (rule) {
		case 1:
			temp = "Banker POK " + banker.getScore() + " ";
			if (banker.getBounce() > 1)
				temp += banker.getBounce() + " bounces";
			return "L/" + temp + "!/1///" + getProfit();
		case 2:
			temp = "Player POK " + player.getScore() + " ";
			if (player.getBounce() > 1)
				temp += player.getBounce() + " bounces";
			return "W/" + temp + "!/2///" + getProfit();
		case 11:
			return "L/Banker win : flush!/11///" + getProfit();
		case 12:
			return "W/Player win : flush!/12///" + getProfit();
		case 21:
			return "L/Banker win : Three tiles!/21///" + getProfit();
		case 22:
			return "W/Player win : Three tiles!/22///" + getProfit();
		case 31:
			return "L/Banker win : Three of a kind!!/31///" + getProfit();
		case 32:
			return "W/Player win : Three of a kind!!/32///" + getProfit();
		case 41:
			return "L/Banker win : Straight flush!!/41///" + getProfit();
		case 42:
			return "W/Player win : Straight flush!!/42///" + getProfit();
		case 81:
			temp = "Banker win : " + banker.getScore() + " point ";
			if (banker.getBounce() > 1)
				temp += banker.getBounce() + " bounces";
			return "L/" + temp + "!/81///" + getProfit();
		case 82:
			temp = "Player win : " + player.getScore() + " point ";
			if (player.getBounce() > 1)
				temp += player.getBounce() + " bounces";
			return "W/" + temp + "!/82///" + getProfit();
		case 99:
			System.out.println("DRAW !");
			return "D/DRAW !/99///" + getProfit();
		default:
			return "N/-/0///" + getProfit();
		}
	}

	/** check status of win state. */
	public boolean checkStatus(int rule) {
		switch (rule) {
		case 1:
			System.out.print("Banker POK " + banker.getScore() + " ");
			if (banker.getBounce() > 1)
				System.out.print(banker.getBounce() + " bounces");
			System.out.println("!");
			return true;
		case 2:
			System.out.print("Player POK " + player.getScore() + " ");
			if (player.getBounce() > 1)
				System.out.print(player.getBounce() + " bounces");
			System.out.println("!");
			return true;
		case 11:
			System.out.println("Banker win : flush!");
			return true;
		case 12:
			System.out.println("Player win : flush!");
			return true;
		case 21:
			System.out.println("Banker win : Three tiles!");
			return true;
		case 22:
			System.out.println("Player win : Three tiles!");
			return true;
		case 31:
			System.out.println("Banker win : Three of a kind!!");
			return true;
		case 32:
			System.out.println("Player win : Three of a kind!!");
			return true;
		case 41:
			System.out.println("Banker win : Straight flush!!");
			return true;
		case 42:
			System.out.println("Player win : Straight flush!!");
			return true;
		case 81:
			System.out.print("Banker win : " + banker.getScore() + " point ");
			if (banker.getBounce() > 1)
				System.out.print(banker.getBounce() + " bounces");
			System.out.println("!");
			return true;
		case 82:
			System.out.print("Player win : " + player.getScore() + " point ");
			if (player.getBounce() > 1)
				System.out.print(player.getBounce() + " bounces");
			System.out.println("!");
			return true;
		case 99:
			System.out.println("DRAW !");
			return true;
		default:
			return false;
		}
	}
}
